package com.hd123.rumba.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
  /**
   * @param date
   * @param field:
   *          one of followings: Calendar.YEAR, MONTH, DATE, HOUR, MINUTE,
   *          SECOND, WEEK_OF_YEAR
   * @return
   */
  public static Date truncate(Date date, int field) {
    if (date == null)
      return null;
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    switch (field) {
    case Calendar.YEAR:
      c.clear(Calendar.MONTH);
    case Calendar.MONTH:
      c.set(Calendar.DAY_OF_MONTH, 1);
    case Calendar.DATE:
      c.set(Calendar.HOUR_OF_DAY, 0);
    case Calendar.HOUR:
      c.clear(Calendar.MINUTE);
    case Calendar.MINUTE:
      c.clear(Calendar.SECOND);
    case Calendar.SECOND:
      c.clear(Calendar.MILLISECOND);
      break;
    case Calendar.WEEK_OF_MONTH:
    case Calendar.WEEK_OF_YEAR:
      c.clear(Calendar.MILLISECOND);
      c.clear(Calendar.SECOND);
      c.clear(Calendar.MINUTE);
      c.set(Calendar.HOUR_OF_DAY, 0);
      int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
      c.add(Calendar.DATE, -(dayOfWeek - 1));
      break;
    default:
      throw new IllegalArgumentException("Unknown field " + field);
    }
    return c.getTime();
  }

  /**
   * 取得日期的时间部分。
   * 
   * @param date
   * @return
   */
  public static Date getTimePart(Date date) {
    Calendar c1 = Calendar.getInstance();
    c1.setTime(date);
    c1.clear(Calendar.YEAR);
    c1.clear(Calendar.MONTH);
    c1.set(Calendar.DAY_OF_MONTH, 1);
    return c1.getTime();
  }

  /**
   * 将指定日期部分与时间部分相加。
   * 
   * @param date
   *          指定日期部分。
   * @param time
   *          指定时间部分。
   * @return
   */
  public static Date addDateAndTime(Date date, Date time) {
    return new Date(date.getTime() + time.getTime()
        + Calendar.getInstance().get(Calendar.ZONE_OFFSET));
  }

  /**
   * @param date
   * @param field:
   *          Calendar.FIELD
   * @param amount
   * @return
   */
  public static Date add(Date date, int field, int amount) {
    if (date == null)
      return null;
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(field, amount);
    return c.getTime();
  }

  /**
   * 取出日期的一部分。
   * 
   * @param date
   * @param field
   *          Calendar.FIELD
   * @return
   */
  public static int get(Date date, int field) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    return c.get(field);
  }

  public static int offset = Calendar.getInstance().getTimeZone().getOffset(new Date().getTime());

  public static Date sql2util(ResultSet rs, String field) throws SQLException {
    return new Date(rs.getDate(field).getTime() + rs.getTime(field).getTime() + offset);
  }

  /**
   * Returns a String describing the amount of time between now (current system
   * time) and the passed in date time. Example output is "5 hours ago" or
   * "Yesterday at 3:30 pm"
   * 
   * @param date
   *          the Date to compare the current time with.
   * @return a description of the difference in time, ie: "5 hours ago" or
   *         "Yesterday at 3:30pm"
   */

  public static String dateToText(Date date) {
    if (date == null) {
      return "";
    }

    long delta = System.currentTimeMillis() - date.getTime();
    if (delta < MINUTE) {
      return "1分钟之内";
    }
    if (delta < 2 * MINUTE) {
      return "1分钟之前";
    }
    if (delta < HOUR) {
      return delta / MINUTE + "分钟之前";
    }
    Date today = truncate(new Date(), Calendar.DATE);
    if (date.getTime() >= today.getTime()) {
      long hour = delta / HOUR;
      long minute = (delta - hour * HOUR) / MINUTE;
      return hour + "小时" + minute + "之前";
    }
    Date yesterday = add(today, Calendar.DATE, -1);
    if (date.getTime() > yesterday.getTime()) {
      return new SimpleDateFormat("昨天HH:mm").format(date);
    }
    Date theDayBefore = add(today, Calendar.DATE, -2);
    if (date.getTime() > theDayBefore.getTime()) {
      return new SimpleDateFormat("前天HH:mm").format(date);
    }
    return new SimpleDateFormat("EEEE, MM-dd HH:mm").format(date);
  }

  private static final long SECOND = 1000;
  private static final long MINUTE = 60 * SECOND;
  private static final long HOUR = 60 * MINUTE;
}
